package com.leaves.common.util.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: LEAVES
 * @Version 1.0
 * @Date: 2023年01月18日  11时04分23秒
 * @Description:
 */
public class DateUtils {

    public static final String YYYY_MM_DD__ = "yyyyMMdd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_ = "yyyy年MM月dd日";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_HH_MM_SS_ = "yyyy年MM月dd日 HH时mm分ss秒";

    /**
     * 根据时间字符串转换成
     *
     * @param currentDate
     * @param formateStr
     * @return
     */
    public static Date dateStrConvertDate(String currentDate, String formateStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formateStr);
        LocalDateTime localDateTime = LocalDateTime.parse(currentDate, dateTimeFormatter);
        return localDateTimeConvertDate(localDateTime);
    }

    /**
     * 根据date转换成localDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime dateConvertLocalDateTime(Date date) {
        LocalDateTime localDateTime = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
        return localDateTime;
    }

    /**
     * 根据localDateTime转换成date
     *
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeConvertDate(LocalDateTime localDateTime) {
        return Date.from(getZonedDateTimeByLocalDateTime(localDateTime).toInstant());
    }

    /**
     * 根据localDateTime转换成ZonedDateTime对象，用于把localDatTime转成Date
     *
     * @param localDateTime
     * @return
     */
    public static ZonedDateTime getZonedDateTimeByLocalDateTime(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        return zonedDateTime;
    }

    /**
     * 判断开始时间是小于结束时间
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean judgeTime(Date beginTime, Date endTime) {
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (!begin.after(end)) {
            return false;
        }
        return true;

    }

    /**
     * 判断开始时间是小于结束时间
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean judgeTime(String startTime, String endTime) {
        if (startTime.compareTo(endTime) < 0) {
            return true;
        }
        return false;
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(betweenDays));
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(String smdate, String bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(betweenDays));
    }

    /**
     * 计算两个时间相差的时分秒
     *
     * @param inDate
     * @param nowDate
     * @return
     */
    public static TimeDifference getMinute(LocalDateTime inDate, LocalDateTime nowDate) {
        Duration duration = Duration.between(inDate, nowDate);
        long hour = duration.getSeconds() / ChronoUnit.HOURS.getDuration().getSeconds();
        long minute = (duration.getSeconds() - ChronoUnit.HOURS.getDuration().getSeconds() * hour) / ChronoUnit.MINUTES.getDuration().getSeconds();
        long second = (duration.getSeconds() - ChronoUnit.HOURS.getDuration().getSeconds() * hour) - minute * ChronoUnit.MINUTES.getDuration().getSeconds();
        TimeDifference timeDifference = new TimeDifference();
        timeDifference.setHour(hour);
        timeDifference.setMinute(minute);
        timeDifference.setSecond(second);
        return timeDifference;
    }

    /**
     * 两个时间相差多少小时
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long getHoursBetween(String startTime, String endTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long seconds = 0L;
        try {
            Date startDate = formatter.parse(startTime);
            long msStart = startDate.getTime();

            Date endDate = formatter.parse(endTime);
            long msEnd = endDate.getTime();

            seconds = (msEnd - msStart) / (1000 * 60 * 60);

        } catch (ParseException e) {
            e.printStackTrace();

        }
        return seconds;

    }

    /**
     * 将时分秒转为小时
     *
     * @param timeDifference
     * @return
     */
    public static double minuteTurnedIntoAndHour(TimeDifference timeDifference) {
//        DecimalFormat df = new DecimalFormat("0.00");
        long second = timeDifference.getSecond();
        long minute = timeDifference.getMinute();
        long hour = timeDifference.getHour();
//        double ds = 0;
        double dm = 0;
        double dh = 0;
        if (second > 0) {
            dm = minute + (second / 60.0);
            if (dm >= 60) {
                dm = 0;
                dh = hour + 1;
            } else {
                dh = hour;
            }
        }
        if (dm > 0) {
            dh = dh + (dm / 60);
        }
//        dh = Double.parseDouble(df.format(dh));
        return dh;
    }
}
